﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Globalization;
using Telerik.Web.UI;

namespace Medianamik.UI.Controls
{
    /// <summary>
    /// Contrôle permettant de sélectionner une date sous la forme d'un calendrier
    /// </summary>
    public class Calendar : BaseMedianamikControl
    {
        protected RadCalendar radDatePicker = new RadCalendar();
        protected TextBox hiddenTextBox = new TextBox();

        protected override void CreateControlHierarchy(bool pUseDataSource)
        {

            Controls.Add(radDatePicker);
            radDatePicker.ID = "datePickerCal";

            if (IsRequired)
            {
                Controls.Add(hiddenTextBox);
                hiddenTextBox.ID = ControlToValidateId;
            }

            if (pUseDataSource)
            {
                radDatePicker.EnableMultiSelect = false;
                radDatePicker.ShowRowHeaders = false;
                radDatePicker.UseColumnHeadersAsSelectors = false;
                radDatePicker.EnableMonthYearFastNavigation = false;
                radDatePicker.FastNavigationSettings.CancelButtonCaption = Translator.Translate("Medianamik_Controls", "CancelButton");
                radDatePicker.FastNavigationSettings.TodayButtonCaption = Translator.Translate("Medianamik_Controls", "TodayButton");
                //radDatePicker.Skin = "Medianamik";
                //radDatePicker.EnableEmbeddedSkins = false;
                radDatePicker.CssClass = "DatePickerCal";

                //Ajouter un textbox invisible pour la validation du requis
                if (IsRequired)
                {
                    hiddenTextBox.Attributes.Add("style", "DISPLAY: none;");
                }

                var date = (DateTime?)DataSource;
                if (date.HasValue)
                {
                    radDatePicker.SelectedDate = date.Value;
                    radDatePicker.FocusedDate = radDatePicker.SelectedDate;
                }
                else
                {
                    radDatePicker.FocusedDate = DateTime.Today;
                }

                radDatePicker.Width = ControlInfo.Width.HasValue 
                                    ? ControlInfo.Width.Value 
                                    : WIDTH;
            }

            //TEMPORAIRE....
            /*
            helpImage.ImageUrl = "/medianamik/images/icones/ico_aide.gif";
            helpImage.ID = "help";
            helpTooltip.TargetControlID = helpImage.ClientID;
            helpTooltip.RelativeTo = ToolTipRelativeDisplay.Element;
            helpTooltip.Position = ToolTipPosition.MiddleRight;
            helpTooltip.ShowEvent = ToolTipShowEvent.OnMouseOver;
        
            Controls.Add(helpImage);
            Controls.Add(helpTooltip);
            */


            if (IsRequired)
            {
                radDatePicker.ClientEvents.OnDateSelected = "OnDateSelected" + radDatePicker.UniqueID;

                //Pour la validation
                string script = String.Format("function OnDateSelected{0}(calInstance, args)[var tb=document.getElementById(\"{1}\");if(args.get_renderDay().get_isSelected())[tb.value=args.get_renderDay().get_date().toString();]else[tb.value=\"\";]]", radDatePicker.UniqueID, hiddenTextBox.ClientID);
                script = script.Replace('[', '{');
                script = script.Replace(']', '}');
                ScriptManager.RegisterClientScriptBlock(this, GetType(), "RetrieveSelectImageScript" + radDatePicker.ClientID.Replace("-", ""), script, true);
            }
        }

        protected override bool WrapContentInParagraph
        {
            get
            {
                return false;
            }
        }

        public override Type TypeOfValue
        {
            get { return typeof(DateTime?); }
        }

        public override object GetValue()
        {

            EnsureChildControls();
            return radDatePicker.SelectedDate;

        }

        public override void SetValue(object value)
        {
            EnsureChildControls();
            radDatePicker.SelectedDate = ((DateTime)value);
            radDatePicker.FocusedDate = ((DateTime)value);
        }
    }
}
